import 'package:flutter/material.dart';
import 'package:flutter_app/utils/DialogUtils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:pinput/pinput.dart';

import '../../../r.dart';
import 'set_logic.dart';

/**
 * @Description: 设置页面
 * @Author: 下雨了
 * @CreateDate: 2021-10-22 10:00
 * @UpdateUser: 更新者
 * @UpdateDate: 2022-6-22 11:35:57
 * @UpdateRemark: GetX处理
 * @Version: 1.1
 */
///
class SetPage extends StatefulWidget {
  final String title;

  const SetPage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  _SetPageState createState() => _SetPageState();
}

class _SetPageState extends State<SetPage> {
  final logic = Get.put(SetLogic());
  final state = Get.find<SetLogic>().state;

  @override
  Widget build(BuildContext context) {
    final defaultPinTheme = PinTheme(
      width: 42.r,
      height: 42.r,
      textStyle: TextStyle(fontSize: 20.sp, color: MyColors.FF333333),
      decoration: BoxDecoration(
        color: MyColors.FFF1F1F1,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: Colors.transparent),
      ),
    );

    final focusedPinTheme = defaultPinTheme.copyWith(
      width: 45.r,
      height: 45.r,
      decoration: defaultPinTheme.decoration!.copyWith(
        border: Border.all(color: MyColors.FF2763B9),
      ),
    );

    return GetBuilder<SetLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF1F1F1,
        appBar: MyAppBar(context, title: "设置"),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.fromLTRB(17.r, 13.5.r, 17.r, 0),
                child: Material(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  child: Column(
                    children: [
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('清除缓存', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                              child: RichText(text: TextSpan(
                                children: [
                                  TextSpan(
                                      text: state.dataNum,
                                      style: TextStyle(color: MyColors.FF999999, fontSize: 12.sp)),
                                  WidgetSpan(
                                    child: Padding(
                                      padding: EdgeInsets.only(left: 10.r),
                                      child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                                    ),
                                  ),
                                ],
                              )),
                            ),
                          ],
                        ),
                        onTap: () {
                          dialogUtils.show(
                            context: context,
                            title: "提示",
                            content: "确定清除缓存？",
                            callbackOk: () {
                              logic.clearAllCache();
                            },
                          );
                        },
                      ),
                      Divider(
                        height: 0.5,
                        color: MyColors.FFF5F5F5,
                      ),
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('推送通知', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 12.r,),
                              child: Switch(value: state.push, onChanged: (bool b) {
                                setState(() {
                                  state.push = b;
                                });
                                logic.changePushState(b);
                              }),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(17.r, 12.r, 17.r, 0),
                child: Material(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  child: InkWell(
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                          child: Text('修改密码', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                        ),
                        Padding(
                          padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                          child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                        ),
                      ],
                    ),
                    onTap: () {
                      logic.goToPasswordChangePage();
                    },
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(17.r, 12.r, 17.r, 0),
                child: Material(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  child: InkWell(
                    borderRadius: BorderRadius.all(Radius.circular(5.r)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                          child: Text('本地相册权限', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                        ),
                        Padding(
                          padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                          child: RichText(text: TextSpan(
                            children: [
                              TextSpan(text: state.pinState.value ? '已开通' : '未开通', style: TextStyle(color: MyColors.FF999999, fontSize: 12.sp)),
                              WidgetSpan(
                                child: Padding(
                                  padding: EdgeInsets.only(left: 10.r),
                                  child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                                ),
                              ),
                            ],
                          )),
                        ),
                      ],
                    ),
                    onTap: () {
                      if (state.pinState.value) {
                        return toast.show("已经开通权限");
                      }
                      // 制空错误信息
                      state.pinError.value = "";

                      showDialog(context: context, builder: (BuildContext context) {
                        return Dialog(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(13.33.r),
                            ),
                            insetPadding: EdgeInsets.symmetric(horizontal: 24.0.r, vertical: 24.0.r),
                            child: IntrinsicHeight(
                              child: Container(
                                child: Column(
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(top: 17.5.r),
                                      child: Text("请输入授权码", style: TextStyle(color: Colors.black, fontSize: 16.sp, fontWeight: FontWeight.w500),),
                                    ),
                                    Obx(() {
                                      return Container(
                                        margin: EdgeInsets.only(top: 11.5.r, left: 16.r, right: 16.r),
                                        child: Text(state.pinError.value,
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(color: MyColors.FFE5261E, fontSize: 14.sp, fontWeight: FontWeight.w500),),
                                      );
                                    }),
                                    Container(
                                      margin: EdgeInsets.only(top: 11.5.r, left: 16.r, right: 16.r,),
                                      height: 45.r,
                                      child: Pinput(
                                        length: 6,
                                        defaultPinTheme: defaultPinTheme,
                                        focusedPinTheme: focusedPinTheme,
                                        autofocus: true,
                                        onCompleted: (value) {
                                          logic.authDevice(value);
                                        },
                                      ),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(top: 11.5.r),
                                      height: 45.r,
                                      child: TextButton(
                                        child: Text("确认", style: TextStyle(color: MyColors.FF2763B9, fontSize: 14.sp, fontWeight: FontWeight.w500),),
                                        style: ButtonStyle(padding: MaterialStateProperty.all(EdgeInsets.all(0)),),
                                        onPressed: () {
                                          Navigator.of(context).pop('ok');
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            )
                        );
                      });
                    },
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(17.r, 12.r, 17.r, 0),
                child: Material(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  child: Column(
                    children: [
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('版本号', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                              child: RichText(text: TextSpan(
                                children: [
                                  TextSpan(
                                      text: 'v' + state.version,
                                      style: TextStyle(color: MyColors.FF999999, fontSize: 12.sp)),
                                ],
                              )),
                            ),
                          ],
                        ),
                        onTap: () {},
                      ),
                      Divider(
                        height: 0.5,
                        color: MyColors.FFF5F5F5,
                      ),
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5.r)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('意见反馈', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                              child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                            ),
                          ],
                        ),
                        onTap: () {
                          logic.goToSuggestionPage();
                        },
                      )
                    ],
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(17.r, 12.r, 17.r, 0),
                child: Material(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  child: Column(
                    children: [
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('用户协议和隐私政策', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                              child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                            ),
                          ],
                        ),
                        onTap: () {
                          logic.goToAgreementWebViewPage();
                        },
                      ),
                      Divider(
                        height: 0.5,
                        color: MyColors.FFF5F5F5,
                      ),
                      InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 24.r, top: 20.r, bottom: 20.r),
                              child: Text('应用权限说明', style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp),),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 24.r, top: 20.r, bottom: 20.r),
                              child: Image.asset(R.assetsImgIcon48, width: 7.r,),
                            ),
                          ],
                        ),
                        onTap: () {
                          logic.goToPermissionWebViewPage();
                        },
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 50.r, bottom: 20.r),
                child: TextButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all<Color>(MyColors.FFF03F35),
                    padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                    minimumSize: MaterialStateProperty.all(Size(120.r, 40.r)),
                  ),
                  child: Text('立即退出', style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 14.sp),),
                  onPressed: () {
                    dialogUtils.show(
                      context: context,
                      title: "提示",
                      content: "是否确认退出？",
                      callbackOk: () {
                        logic.loginOut();
                      },
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<SetLogic>();
    super.dispose();
  }
}
